package com.wap.framework.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wap.framework.model.User;
import com.wap.framework.service.UserManageService;

public class UserManageAction extends DispatchAction {

	private UserManageService userManageService;

	public void setUserManageService(UserManageService userManageService) {
		this.userManageService = userManageService;
	}

	@SuppressWarnings("unchecked")
	public ActionForward listByStatus(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String status = request.getParameter("status");
		if("".equals(status)) {
			status = (String) request.getAttribute("state");
		}
		status = ("".equals(status) || status == null) ? "-1" : status;
		request.setAttribute("state", Integer.valueOf(status));
		
		//显示全部用户
		if("-1".equals(status)) {
			return list(mapping, form, request, response);
		}
		
		String currentPage 	= request.getParameter("currentPage");
		if (currentPage == null || "".equals(currentPage)) {
			currentPage = "1";
		}
		List<Object> result = userManageService.listUsersByStatus(Integer
				.valueOf(currentPage), Integer.valueOf(status));
		request.setAttribute("users", (List<User>) result.get(0));
		request.setAttribute("pageHelper", result.get(1));		
		request.setAttribute("method", "listByStatus");
		
		return mapping.findForward("fwd_mgr_user");
	}

	@SuppressWarnings("unchecked")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String status = request.getParameter("status");
		if("".equals(status)) {
			status = (String) request.getAttribute("state");
		}
		status = ("".equals(status) || status == null) ? "-1" : status;
		request.setAttribute("state", Integer.valueOf(status));
		
		String currentPage = request.getParameter("currentPage");
		if (currentPage == null || "".equals(currentPage)) {
			currentPage = "1";
		}

		List<Object> result = userManageService.listUsers(Integer
				.valueOf(currentPage));
		request.setAttribute("users", (List<User>) result.get(0));
		request.setAttribute("pageHelper", result.get(1));
		request.setAttribute("method", "list");

		return mapping.findForward("fwd_mgr_user");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String[] checkedIds = request.getParameterValues("id");
		String[] uid = new String[checkedIds.length];
		String[] status = new String[checkedIds.length];
		for(int i = 0; i < checkedIds.length; i++){			
			uid[i] = checkedIds[i].substring(0,checkedIds[i].indexOf("@"));
			status[i] = checkedIds[i].substring(checkedIds[i].indexOf("@") + 1);
		}
		Integer[] i_uid = (Integer[])ConvertUtils.convert(uid, Integer.class);
		Integer[] i_status = (Integer[])ConvertUtils.convert(status, Integer.class);
		
		
		this.userManageService.updateUser(i_uid, i_status);
		
		request.setAttribute("state", i_status[0] == 0 ? 1 : 0);
		return listByStatus(mapping, form, request, response);
	}
}
